/* Load in the shapes */
use ${additionaldata}grk_data, clear
bys GRUNNKRETS (Shape_area): keep if _n == _N /* In case of duplicates, only keep the object with the largest geographical shape */
tempfile grkdata
keep if KOMM == 1201 // only keeping Bergen municipality
save `grkdata'

/* Load in the data */
use ${newdata}carownership_dataset_bergen, clear

gen kommune = floor(grk/10000)
keep if kommune == 1201 // Only keeping Bergen

keep if year == 2014

gen hh = 1

/* Collapsing down to grunnkrets level */
gen d_status1 = ($trmgroup == 1)
gen d_status2 = ($trmgroup == 2)
gen d_status3 = (d_status1 != 1 & d_status2 != 1)


collapse (sum) antpers_i_regstat_famnr hh d_status1 d_status2 d_status3 ///
	(mean) bev ice toll wies wealth children age ///
		  /// d_status1 d_status2 d_status3 ///
	, by( grk year kommune)
	
rename grk GRUNNKRETS
merge m:1 GRUNNKRETS using `grkdata', keep(match using master)	

gen pc_sh = d_status1 / hh
gen npc_sh = d_status2 / hh


	
spmap pc_sh if year == 2014 using ${additionaldata}grk_coordinates, id(_ID) ///
		clmethod(custom) ///
		clbreaks(0 0.15 0.30 0.45 0.60 0.75 0.9 1) ///ocolor(none ..)
		legend(position(2) symysize(*2) symxsize(*2) size(3)) /*title("Paying commuter share", size(*0.8))*/ ///
		name(b3a, replace) graphregion(color(white)) fcolor(Blues2)

graph export "${figures}figB4a.png", as(png) replace
graph export "${figures}figB4a.pdf", as(pdf) replace
		
spmap npc_sh if year == 2014 using ${additionaldata}grk_coordinates, id(_ID) ///
		clmethod(custom) ///
		clbreaks(0 0.15 0.30 0.45 0.60 0.75 0.9 1) ///ocolor(none ..)
		legend(position(2) symysize(*2) symxsize(*2) size(3)) /*title("Non-paying commuter sh.", size(*0.7))*/  ///
		name(b3b, replace) graphregion(color(white))	fcolor(Blues2)	

graph export "${figures}fig3b.png", as(png) replace
graph export "${figures}fig3b.pdf", as(pdf) replace		